class Solution {
public:
    struct Compare//仿函数
    {
        bool operator()(const pair<string,int>& k1,const pair<string,int>& k2)
        {
            return k1.second>k2.second;
        }
    };
    vector<string> topKFrequent(vector<string>& words, int k) {
        map<string,int> mp;
        for(auto str:words)
        {
            mp[str]++;
        }
        vector<pair<string,int>> v(mp.begin(),mp.end());
        stable_sort(v.begin(),v.end(),Compare());//稳定排序
        vector<string> ret;
        for(int i=0;i<k;i++)
        {
            ret.push_back(v[i].first);
        }
        return ret;
    }
};